﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net.MVC;
using Buffalo.SMECommon.DAL.Models;
using Buffalo.SMECommon.DAL;
using Buffalo.Lib.Common;
using Buffalo.Administration.DAL;
using Buffalo.Administration.Enum;
using Buffalo.SMECommon.Enum;

namespace Buffalo.Web.Areas.SMECommon.Controllers
{
    public class PartnerController : BaseController
    {
        #region Partner
        public AjaxStoreResult GetListPartner(int limit, int start, string dir, string sort, string withDefaultItem, string partnerTypeText, string cboCriteria, string txtSearchText)
        {
            try
            {
                int total = 0;
                enuPartnerType partnerType = (enuPartnerType)Enum.Parse(typeof(enuPartnerType), partnerTypeText, true);
                string criteria = cboCriteria.Equals(GeneralConstant.ALL_ITEMS) ? string.Empty : cboCriteria;
                string searchText = string.IsNullOrEmpty(txtSearchText) ? string.Empty : txtSearchText.Trim();

                List<Partner> list = new PartnerDAL().GetListPartner(limit, start, dir, sort, partnerType, criteria, searchText, out total);
                if (Util.ToSafeBool(withDefaultItem))
                    list.Insert(0, new Partner { ID = GeneralConstant.BLANK_ITEM, Name = "..." });

                return new AjaxStoreResult(list, total);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AjaxFormResult SaveCustomerInfo(FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();

            try
            {
                string IDCustomer = values["id"];
                if (string.IsNullOrEmpty(values["Code"]))
                {
                    response.Success = false;
                    response.Errors.Add(new FieldError("Code", "Code is required"));
                    return response;
                }

                Partner customer;
                PartnerDAL partnerDAL = new PartnerDAL();
                if (string.IsNullOrEmpty(IDCustomer) || IDCustomer.Equals("0"))
                {
                    customer = new Partner();
                    customer.ID = IDCustomer;
                }
                else
                {
                    customer = partnerDAL.GetPartner(IDCustomer);
                }
                customer.Code = values["Code"];
                customer.Name = values["Name"];
                customer.Note = values["Note"];
                customer.PartnerType = Util.ToSafeInt( values["PartnerType"]);
                customer.IDPartnerCategory = values["IDPartnerCategory"];

                partnerDAL.SaveCustomer(customer);

                response.ExtraParams["newID"] = customer.ID;
            }
            catch (Exception e)
            {
                response.Success = false;
                response.ExtraParams["msg"] = e.Message;
            }

            return response;
        }
        public AjaxFormResult SaveSupplierInfo(FormCollection values)
        {
            AjaxFormResult response = new AjaxFormResult();

            try
            {
                string IDSupplier = values["id"];
                if (string.IsNullOrEmpty(values["Code"]))
                {
                    response.Success = false;
                    response.Errors.Add(new FieldError("Code", "Code is required"));
                    return response;
                }

                Partner supplier;
                PartnerDAL partnerDAL = new PartnerDAL();
                if (string.IsNullOrEmpty(IDSupplier) || IDSupplier.Equals("0"))
                {
                    supplier = new Partner();
                    supplier.ID = IDSupplier;
                }
                else
                {
                    supplier = partnerDAL.GetPartner(IDSupplier);
                }
                supplier.Code = values["Code"];
                supplier.Name = values["Name"];
                supplier.Note = values["Note"];
                supplier.PartnerType = Util.ToSafeInt(values["PartnerType"]);
                supplier.IDPartnerCategory = values["IDPartnerCategory"];

                partnerDAL.SaveSupplier(supplier);

                response.ExtraParams["newID"] = supplier.ID;
            }
            catch (Exception e)
            {
                response.Success = false;
                response.ExtraParams["msg"] = e.Message;
            }

            return response;
        }
        public AjaxStoreResult SavePartners()
        {
            AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save);
            try
            {
                new PartnerDAL().SavePartners(HttpContext.Request["data"]);
            }
            catch (Exception e)
            {
                ajaxStoreResult.SaveResponse.Success = false;
                ajaxStoreResult.SaveResponse.Message = e.Message;
            }

            return ajaxStoreResult;
        }
        public ActionResult CustomerEditor()
        {
            ViewData["IDCustomer"] = HttpContext.Request["IDCustomer"] ?? string.Empty;
            return this.View();
        }
        public ActionResult SupplierEditor()
        {
            ViewData["IDSupplier"] = HttpContext.Request["IDSupplier"] ?? string.Empty;
            return this.View();
        }

        public ActionResult CustomerList()
        {
            return CheckPermission("Customer");
        }
        public AjaxStoreResult GetPartner(string IDPartner)
        {
            if (!string.IsNullOrEmpty(IDPartner))
            {
                if (IDPartner.Equals("0"))
                {
                    var query = new Partner();
                    query.ID = "0";
                    return new AjaxStoreResult(query, 1);
                }
                else
                {
                    var query = new PartnerDAL().GetPartner(IDPartner);
                    int total = query != null ? 1 : 0;
                    return new AjaxStoreResult(query, total);
                }
            }

            return null;
        }
        public ActionResult SupplierList()
        {
            return CheckPermission("Supplier");
        }
        public ActionResult EmployeeList()
        {
            return CheckPermission("Employee");
        }

        #endregion
        private ActionResult CheckPermission(string featureName)
        {
            if (!HttpContext.User.Identity.IsAuthenticated)
                return this.RedirectToAction("ReLogin", "Home");

            bool hasViewPermision = new PermissionDAL().HasPermission(GeneralVar.gLoggedUserName, featureName, enuPermissionType.View);
            if (hasViewPermision)
                return this.View();
            return null;
        }
    }
}
